package com.hc.tenant.service.impl;

import java.util.List;

import com.hc.common.core.exception.tenant.TenantException;
import com.hc.common.core.utils.DateUtils;
import com.hc.common.security.service.TokenService;
import com.hc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hc.tenant.mapper.MenusMapper;
import com.hc.tenant.domain.Menus;
import com.hc.tenant.service.IMenusService;

/**
 * 菜谱Service业务层处理
 * 
 * @author yongjianz
 * @date 2021-02-06
 */
@Service
public class MenusServiceImpl implements IMenusService 
{
    @Autowired
    private MenusMapper menusMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询菜谱
     * 
     * @param menusId 菜谱ID
     * @return 菜谱
     */
    @Override
    public Menus selectMenusById(Long menusId)
    {
        return menusMapper.selectMenusById(menusId);
    }

    /**
     * 查询菜谱列表
     * 
     * @param menus 菜谱
     * @return 菜谱
     */
    @Override
    public List<Menus> selectMenusList(Menus menus)
    {
        return menusMapper.selectMenusList(menus);
    }

    /**
     * 新增菜谱
     * 
     * @param menus 菜谱
     * @return 结果
     */
    @Override
    public int insertMenus(Menus menus)
    {
        menus.setCreateTime(DateUtils.getNowDate());
        Menus selectMenus = menusMapper.selectUserByMenusName(menus.getMenusName());
        if(selectMenus != null)
            throw new TenantException("菜谱已存在");
        LoginUser loginUser = tokenService.getLoginUser();
        menus.setCreateBy(loginUser.getUsername());
        return menusMapper.insertMenus(menus);
    }

    /**
     * 修改菜谱
     * 
     * @param menus 菜谱
     * @return 结果
     */
    @Override
    public int updateMenus(Menus menus)
    {
        menus.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser();
        menus.setUpdateBy(loginUser.getUsername());
        return menusMapper.updateMenus(menus);
    }

    /**
     * 批量删除菜谱
     * 
     * @param menusIds 需要删除的菜谱ID
     * @return 结果
     */
    @Override
    public int deleteMenusByIds(Long[] menusIds)
    {
        return menusMapper.deleteMenusByIds(menusIds);
    }

}
